package com.hxjb.seckill4spring.config;

import com.huamo.base.log.RequestParser;
import com.huamo.base.log.Requestor;
import com.huamo.mq.MQClient;
import com.huamo.mq.MQManager;
import com.huamo.mq.Message;
import com.huamo.mq.impl.ConfigurationManager;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.Map;


/**
 * Created by wt on 2017/11/21.
 */
public class LogSpringActionHandler extends HandlerInterceptorAdapter {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public LogSpringActionHandler() {
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        RequestParser parser = new RequestParser();
        Requestor requestor = null;
        parser.parser(request);
        requestor = parser.toRequestorObject();

        if(this.log.isInfoEnabled()) {
            Map map = request.getParameterMap();
            StringBuilder sbtest = new StringBuilder();
            Iterator var8 = map.keySet().iterator();

            while(var8.hasNext()) {
                Object obj = var8.next();
                sbtest.append(obj).append("=").append(request.getParameter(obj.toString())).append("&");
            }

            this.log.info(request.getRequestURL().toString() + "?" + sbtest.toString());
        }

        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

}
